<%@ page language="java" contentType="text/html; charset=UTF-8"
    import="ecs.dogear.app.*"
    import="java.util.*"
    pageEncoding="UTF-8"%>
<%!
    String comment2json(Comment comment){
	    StringBuilder sb = new StringBuilder();
	    sb.append("{id:'");
	    sb.append(comment.getCommentId());
	    sb.append("',comment:'");
	    sb.append(StringUtil.escape(comment.getContent()));
	    sb.append("',userId:'");
	    sb.append(comment.getUserId());
	    sb.append("',userName:'");
	    sb.append(CommenterDatabase.getUser(comment.getUserId()).getName());
	    sb.append("',replies:[");
	    for(int i=0;i < comment.getReplies().size();i++){
	    	if(i!=0)
	    		sb.append(",");
	    	String json = comment2json(comment.getReplies().get(i));
	    	sb.append(json);
	    }
	    sb.append("]}");
	    return sb.toString();
    }
%>
<%
    String url = request.getParameter("url");
    ArrayList<Comment> comments = CommenterDatabase.getCommentsOfUrl(url);
    StringBuilder sb = new StringBuilder("[");
    for(int i = 0;i < comments.size();i++){
    	if(i!=0)
    		sb.append(",");
    	sb.append(comment2json(comments.get(i)));
    }
    sb.append("]");
    String handler = request.getParameter("callback");
    out.println(handler + "(" +sb.toString()+")");
%>